<?php
header("Content-Type: application/json; charset=UTF-8");
require_once 'db.php';

// 获取POST数据
$user_id = isset($_POST['user_id']) ? intval($_POST['user_id']) : 0;
$type = $_POST['type'] ?? '';
$title = $_POST['title'] ?? '';
$content = $_POST['content'] ?? '';
$images = $_POST['images'] ?? '';

// 验证必填数据
if ($user_id <= 0 || empty($type) || empty($title) || empty($content)) {
    echo json_encode([
        'status' => 'error',
        'message' => '请填写必填信息'
    ]);
    exit;
}

// 验证反馈类型
$allowedTypes = ['feature', 'bug', 'other'];
if (!in_array($type, $allowedTypes)) {
    echo json_encode([
        'status' => 'error',
        'message' => '无效的反馈类型'
    ]);
    exit;
}

try {
    // 开始事务
    $conn->begin_transaction();

    // 插入反馈数据
    $sql = "INSERT INTO feedbacks (user_id, type, title, content, images, status) 
            VALUES (?, ?, ?, ?, ?, 0)";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("issss", $user_id, $type, $title, $content, $images);

    if (!$stmt->execute()) {
        throw new Exception("提交反馈失败");
    }

    // 提交事务
    $conn->commit();

    echo json_encode([
        'status' => 'success',
        'message' => '反馈提交成功'
    ]);

} catch (Exception $e) {
    // 回滚事务
    $conn->rollback();
    echo json_encode([
        'status' => 'error',
        'message' => $e->getMessage()
    ]);
} finally {
    if (isset($stmt)) $stmt->close();
    $conn->close();
}
?> 